﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGinnBPM.Lib.Messages;
using NGinnBPM.MessageBus;
using NGinnBPM.Lib.Interfaces;

namespace NGinnBPM.Runtime
{
    /// <summary>
    /// Handler for external task control messages (from NGinnBPM.Lib.Messages namespace)
    /// </summary>
    public class ExternalControlMessageHandler : IMessageConsumer<CompleteTask>, IMessageConsumer<CancelTask>, IMessageConsumer<FailTask>
    {
        private INGEnvironment _env;
        private IMessageBus _bus;

        public ExternalControlMessageHandler(INGEnvironment environment, IMessageBus bus)
        {
            _env = environment;
            _bus = bus;
        }

        #region IMessageConsumer<CompleteTask> Members

        public void Handle(CompleteTask message)
        {
            _env.ForceCompleteTask(message.InstanceId, message.UpdatedData, message.CompletedBy);
        }

        #endregion

        #region IMessageConsumer<CancelTask> Members

        public void Handle(CancelTask message)
        {
            _env.CancelTaskInstance(message.InstanceId);
        }

        #endregion

        #region IMessageConsumer<FailTask> Members

        public void Handle(FailTask message)
        {
            _env.ForceFailTask(message.InstanceId, message.ErrorInfo);
        }

        #endregion
    }
}
